package com.ywl.study.service;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.transaction.ChainedTransactionManager;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
public class OracleDBConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.ds.oracle.user")
    public DataSourceProperties oracleUserDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource oracleUserDataSource() {
        return oracleUserDataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    public JdbcTemplate oracleUserJdbcTemplate(@Qualifier("oracleUserDataSource") DataSource userDataSource) {
        return new JdbcTemplate(userDataSource);
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.ds.oracle.order")
    public DataSourceProperties oracleOrderDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource oracleOrderDataSource() {
        return oracleOrderDataSourceProperties()
                .initializeDataSourceBuilder()
                .type(HikariDataSource.class)
                .build();
    }

    @Bean
    public JdbcTemplate oracleOrderJdbcTemplate(@Qualifier("oracleOrderDataSource") DataSource orderDataSource) {
        return new JdbcTemplate(orderDataSource);
    }

}
